package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.ValidateCodeService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/ValidateCode")
public class ValidateCodeController {
    @Reference
    private ValidateCodeService validateCodeService;
    @Autowired
    private JedisPool jedisPool;
    @RequestMapping("/orderInfoValidateCode")
    public Result ValidateCode(String telephone){
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        try {
            System.out.println("validateCode:"+validateCode);
           /* SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,validateCode.toString());*/

        } catch (/*Client*/Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        Jedis resource = jedisPool.getResource();
        resource.setex(telephone+RedisMessageConstant.SENDTYPE_ORDER,5*60,validateCode.toString() );
        resource.close();
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
    @RequestMapping("/submitOrder")
    public Result submitOrder(@RequestBody Map map){
       String telephone = (String) map.get("telephone");
        String code = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
       String validateCode = (String) map.get("validateCode");
       if(code!=null&&validateCode!=null&&code.equals(validateCode)) {
           map.put("orderType", Order.ORDERTYPE_WEIXIN);
           Result result;
           try {
               result = validateCodeService.order(map);
               System.out.println("result:"+result);
           } catch (Exception e) {
               e.printStackTrace();
               return new Result(false, MessageConstant.ORDER_FAIL);
           }
           if (result.isFlag()){
            String orderDate = (String) map.get("orderDate");
               try {
                  /* SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,orderDate);*/
                   System.out.println("预约成功");
               } catch (/*Client*/Exception e) {
                   e.printStackTrace();
               }
           }
           return result;
       }else {
           return new Result(false,MessageConstant.VALIDATECODE_ERROR);
       }

    }
    @RequestMapping("/send4Login")
    public Result getLoginCode(String telephone){
        Integer loginCode = ValidateCodeUtils.generateValidateCode(6);
        try {
            System.out.println("loginCode:"+loginCode);
          /*  SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,loginCode.toString());*/
        } catch (/*Client*/Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        Jedis resource = jedisPool.getResource();
        resource.setex(telephone+RedisMessageConstant.SENDTYPE_LOGIN,5*60,loginCode.toString());
        resource.close();
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
